package com.gxa.autoparts.portal.controller;

import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.AfterSale;
import com.gxa.autoparts.core.pojo.BalanceDetails;
import com.gxa.autoparts.core.pojo.Comment;
import com.gxa.autoparts.core.pojo.Order;
import com.gxa.autoparts.portal.service.AfterSaleService;
import com.gxa.autoparts.portal.service.BalanceDetailsService;
import com.gxa.autoparts.portal.service.MyOrderService;
import com.zto.zop.ZopClient;
import com.zto.zop.ZopPublicRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Transactional(rollbackFor = Throwable.class)
@RequestMapping("/myOrder")
@Api(tags = "我的订单")
public class MyOrderController {
    @Autowired
    private MyOrderService myOrderService;
    @Autowired
    private AfterSaleService afterSaleService;
    @Autowired
    private BalanceDetailsService balanceDetailsService;
    @PostMapping("/checkOrder")
    @ApiOperation("查看我的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "long",paramType = "query")
    })
    public Map findAllMyOrder(Long userId){
        List<Order> orders = myOrderService.checkMyOrder(userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(orders);
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
    @ApiOperation("订单已完成后添加评论")
    @PostMapping("/addComment")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "long",paramType = "query"),
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "long",paramType = "query"),
            @ApiImplicitParam(name = "context",value = "评论",required = true,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "star",value = "评星级",required = true,dataType = "int",paramType = "query")

    })
    public Map addComment(Long userId,Long goodsId,String context,Integer star){
        myOrderService.addComment(userId, goodsId, context, star);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("评论成功！");
        return jsonResult.getDataMap();
    }
    @PostMapping("/checkMyComment")
    @ApiOperation("查看我的订单中的评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "long",paramType = "query"),
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "long",paramType = "query")
    })
    public Map checkMyComment(Long userId, Long goodsId) throws BusinessException {
        Comment comment = myOrderService.checkMyComment(userId, goodsId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(comment);
        return jsonResult.getDataMap();
    }
    @PostMapping("/checkZTO")
    public Map checkZTO() throws IOException {
        ZopClient client = new ZopClient("d577e7b5024ad20446e10", "0e8e9457d493666ee2f5adb783e69abb");

        ZopPublicRequest request = new ZopPublicRequest();

        request.setBody("{\"type\":0,\"orderCode\":\"200824000005397109\",\"billCode\":\"73110263178916\"}");

        request.setUrl("https://japi-test.zto.com/zto.open.getOrderInfo");




        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(client.execute(request));
        return jsonResult.getDataMap();
    }
    @PostMapping("/checkMyAfterSale")
    @ApiOperation("查看我的售后订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "long",paramType = "query")

    })
    public Map checkMyAfterSale(Long userId)  {
        List<AfterSale> afterSales = afterSaleService.checkMyAfterSale(userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(afterSales);
        return jsonResult.getDataMap();
    }
    @PostMapping("/addAfterSale")
    @ApiOperation("申请售后")

    public Map addAfterSale(@RequestBody AfterSale afterSale)  {
        afterSaleService.addAfterSale(afterSale);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
    @PostMapping("/count")
    @ApiOperation("“我的”页面数据统计渲染用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "long",paramType = "query")

    })
    public Map count(Long userId )  {
        Map<String, Object> count = myOrderService.count(userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(count);
        return jsonResult.getDataMap();
    }
    @PostMapping("/checkMyBanlanceDetail")
    @ApiOperation("余额明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "long",paramType = "query")

    })
    public Map checkMyBanlanceDetail(Long userId){
        List<BalanceDetails> balanceDetails = balanceDetailsService.selectAll();
        List<BalanceDetails> balanceDetails1 = new ArrayList<>();
        if (balanceDetails.size()>0){
            for (BalanceDetails balanceDetail : balanceDetails) {
                if (balanceDetail.getUid() == userId){
                    balanceDetails1.add(balanceDetail);
                }
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(balanceDetails1);
        return jsonResult.getDataMap();
    }

}
